﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using System.Drawing;
using System.Collections;
using DatingPortal.Models.User;
using DatingPortal.Entities;
using DatingPortal.Tools;

namespace DatingPortal.Controllers
{
    [Authorize(Roles = StringConstants.USERROLE)]
    public class UserController : Controller
    {
        /// <summary>
        /// Display main user page with the statistics, the info and the userpic
        /// </summary>
        public ActionResult Index()
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            UserProperties userProperties = operations.GetUserProperties(User.Identity.Name);
            MainUserPageModel mainUserPageModel = new MainUserPageModel();
            return View(StringConstants.INDEX, GetCurrentTheme(), mainUserPageModel.ConvertFromUserProperties(userProperties));
        }

        /// <summary>
        /// Handling the filling of edit info page
        /// </summary>
        [HttpGet]
        public ActionResult EditInfo()
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            UserProperties userProperties = operations.GetUserProperties(User.Identity.Name);
            EditInfoModel editInfoModel = new EditInfoModel();
            return View(StringConstants.EDITINFO, GetCurrentTheme(),
                editInfoModel.ConvertFromUserProperties(operations.
                GetUserProperties(User.Identity.Name)));
        }

        /// <summary>
        /// Saving changes in user attributes.
        /// </summary>
        [HttpPost]
        public ActionResult EditInfo(HttpPostedFileBase file, EditInfoModel editInfoModel)
        {
            UserProperties userProperties = editInfoModel.ConvertToUserProperties();
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            operations.AddUserProperties(User.Identity.Name, userProperties);
            operations.AddAboutMe(User.Identity.Name, editInfoModel.AboutMe);
            UploadAvatar(file);
            return View(StringConstants.EDITINFO, GetCurrentTheme());
        }

        /// <summary>
        /// Show user picture.
        /// </summary>
        public ActionResult ShowAvatar()
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            var imageData = operations.GetUserAvatar(User.Identity.Name);
            return File(imageData, "image/jpg");
        }

        /// <summary>
        /// This method is used in UploadAvatar and UploadPhotoIntoGallery methods.
        /// </summary>
        private List<byte> PictureToList(HttpPostedFileBase file)
        {
            int fileLength = file.ContentLength;
            List<byte> tempImage = new List<byte>();
            int i = 0;
            while (i != fileLength)
            {
                try
                {
                    tempImage.Add((byte)file.InputStream.ReadByte());
                }
                catch
                {
                    //do nothing
                }
                finally
                {
                    i++;
                }
            }
            return tempImage;
        }

        /// <summary>
        /// Upload user picture.
        /// </summary>
        public ActionResult UploadAvatar(HttpPostedFileBase file)
        {
            if (file != null)
            {
                List<byte> tempImage = PictureToList(file);
                DatingPortalDBOperations operations = new DatingPortalDBOperations();
                operations.AddUserAvatar(User.Identity.Name, tempImage.ToArray());
            }
            return RedirectToAction(StringConstants.EDITINFO);

        }

        /// <summary>
        /// Uploads picture into he gallery.
        /// </summary>
        public ActionResult UploadPhotoIntoGallery(HttpPostedFileBase file)
        {            
            if (file != null)
            {
                List<byte> tempImage = PictureToList(file);
                DatingPortalDBOperations operations = new DatingPortalDBOperations();
                operations.AddPhotoToTheGallery(User.Identity.Name, tempImage.ToArray());
            }
            return RedirectToAction(StringConstants.GALLERY);
        }

        /// <summary>
        /// Displays user's gallery page.
        /// </summary>
        public ActionResult Gallery()
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            GalleryModel gallery = new GalleryModel();
            gallery.Photo = operations.ReturnUserPhotos(User.Identity.Name);
            return View(StringConstants.GALLERY, GetCurrentTheme(), gallery);
        }

        /// <summary>
        /// Shows picture by id.
        /// </summary>
        public ActionResult ShowPicture(long id)
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            var imageData = operations.ReturnPhotoById(id);
            return File(imageData, "image/jpg");
        }

        /// <summary>
        /// Displays users' avatars on the search page. 
        /// </summary>
        public ActionResult DisplayAvatar(String userName)
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            var imageData = operations.GetUserAvatar(userName);
            return File(imageData, "image/jpg");
        }

        /// <summary>
        /// Removes photo from user gallery.
        /// </summary>
        public ActionResult RemovePhotoFromGallery(Int64 id)
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            operations.DeletePhotoById(id);
            return RedirectToAction(StringConstants.GALLERY);
        }

        /// <summary>
        /// Displays user's tags store page.
        /// </summary>
        public ActionResult TagsStore()
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            TagModel tags = new TagModel();
            tags.Tag = operations.ReturnUserTags(User.Identity.Name);
            return View(StringConstants.TAGSSTORE, GetCurrentTheme(), tags);
        }

        /// <summary>
        /// Removes tag by tag id.
        /// </summary>
        public ActionResult RemoveTag(Int64 id)
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            operations.DeleteTagById(id,User.Identity.Name);
            return RedirectToAction(StringConstants.TAGSSTORE);
        }

        /// <summary>
        /// Adds tag to the user store.
        /// </summary>
        public ActionResult AddTag(TagModel tagModel)
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            operations.AddTagToTheStore(User.Identity.Name, tagModel.TagToAdd);
            return RedirectToAction(StringConstants.TAGSSTORE);
        }

        /// <summary>
        /// Deletes profile.
        /// </summary>
        public ActionResult DeleteProfile()
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            operations.DeleteUserProfile(User.Identity.Name);
            Membership.DeleteUser(User.Identity.Name);
            FormsAuthentication.SignOut();
            return RedirectToAction(StringConstants.INDEX, StringConstants.HOME);
        }

        /// <summary>
        /// Displays user community page.
        /// </summary>
        public ActionResult Communities()
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            UserCommunityModel model = new UserCommunityModel();
            model.UserCommunities = operations.ReturnUserCommunities(User.Identity.Name);
            model.AllCommunities = operations.ReturnListOfCommunities();
            return View(StringConstants.COMMUNITIES, 
                GetCurrentTheme(), model);
        }

        /// <summary>
        /// Removes user from community.
        /// </summary>
        public ActionResult RemoveUserFromCommunity(long id)
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            operations.RemoveCommunityFromUserList(id, User.Identity.Name);
            return RedirectToAction(StringConstants.COMMUNITIES);
        }

        /// <summary>
        /// Adds user to community.
        /// </summary>
        public ActionResult AddUserToCommunity(long id)
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            operations.AddUserToCommunity(id, User.Identity.Name);
            return RedirectToAction(StringConstants.COMMUNITIES);
        }

        /// <summary>
        /// Displays search page.
        /// </summary>
        public ActionResult Search()
        {
            return View(StringConstants.SEARCH, GetCurrentTheme());
        }

        public ActionResult SearchByWord(SearchModel model)
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            model.Profiles = (operations.SearchByWord(model) as IList<Profile>);
            return View(StringConstants.SEARCH, GetCurrentTheme(), model);
        }

        public ActionResult ComplexSearch(SearchModel model)
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            model.Profiles = operations.ComplexSearch(model);
            return View(StringConstants.SEARCH, GetCurrentTheme(), model);
        }

        private String GetCurrentTheme()
        {
            DatingPortalDBOperations operations = new DatingPortalDBOperations();
            String currentTheme = StringConstants.BRIGHTTHEME;
            if(Request.IsAuthenticated)
            {
                currentTheme=operations.ReturnUserTheme(User.Identity.Name);
            }
            ViewData["theme"] = StringConstants.STYLESPATH + currentTheme + StringConstants.MAINCSS;
            ViewData["highslide"] = StringConstants.STYLESPATH + currentTheme + StringConstants.HIGHSLIDECSS;
            ViewData["table"] = StringConstants.STYLESPATH + currentTheme + StringConstants.TABLECSS;
            return StringConstants.MASTERNAME;
        }
    }
}
